//
//  MTCCreateContractTYDZController.m
//  XTCloud
//
//  Created by QA on 2017/9/21.
//  Copyright © 2017年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "MTCCreateContractTYDZController.h"
#import "GEInputTableViewCell.h"
#import "MTCSelectClienterViewController.h"
#import "MTCConcreteConfirmationTYDZController.h"

#import "BGRadioView.h"
#import "JGActionSheet.h"

#import "NSDictionary+DataFormat.h"
#import "NSMutableDictionary+Format.h"
#import "GEPickerTextField.h"
#import "NSMutableArray+Format.h"

@interface MTCCreateContractTYDZController ()

@property (nonatomic, strong) NSMutableDictionary *orderInfo;

@end

@implementation MTCCreateContractTYDZController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(submitContractSelector:)];
        self.navigationItem.rightBarButtonItem = leftButton;
    }
    return self;
}

- (void)getPassingParameter:(id)object
{
    if (!object) {
        [self setTitle: @"新建合同"];
    } else {
        [self setTitle: @"修改合同"];
        NSDictionary *dic = object;
        self.orderInfo = [NSMutableDictionary dictionaryWithDictionary:dic];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 10)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 10)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//定制功能--天元站点选择
//- (BOOL)isTYDZModelType
//{
//    NSArray *menuArray = [ModuleAuthorityEntity MR_findByAttribute:@"permissionsMark" withValue:@"P_HTGL_HTXZ_ZDXZ"];
//    if(menuArray.count > 0) return YES;
//    else return NO;
//}

- (void)submitContractSelector:(id)selector
{
    GEInputTableViewCell *geViewCell = (GEInputTableViewCell*)[self.tableView getCellViewInRow:0 inSection:0];
    GEPickerTextField *textField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:0];
    
    UITextField *textField01 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:0];
    UITextField *textField03 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:3 inSection:0];
    UITextField *textField04 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:5 inSection:0];
    
    UITextField *textField09 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:9 inSection:0];
    UITextField *textField10 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:10 inSection:0];
    UITextField *textField07 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:7 inSection:0];
    
    if (!geViewCell.objectData) {
        [self showAlertView:@"请选择客户名称"];
    } else if (textField01.text.length<=0) {
        [self showAlertView:@"请输入合同名称"];
    } else if (!textField.selectData) {
        [self showAlertView:@"请选择合同性质"];
    } else if (textField07.text.length<=0) {
        [self showAlertView:@"请输入施工单位"];
    } else if (textField03.text.length<=0) {
        [self showAlertView:@"请输入签订方量"];
    } else if ([textField03.text floatValue] <0) {
        [self showAlertView:@"签订方量必须大于0方"];
    } else if (textField04.text.length<=0) {
        [self showAlertView:@"请选择签订日期"];
    } else if (!self.orderInfo && textField09.text.length<=0) {
        [self showAlertView:@"请选择编号类型"];
    } else if (!self.orderInfo && textField10.text.length<=0) {
        [self showAlertView:@"请选择归属站点"];
    } else {
        //数据已验证，进行数据封包
        [self newContractDataSealPackage];
    }
}

//新建合同打包
- (void)newContractDataSealPackage
{
    GEInputTableViewCell *geViewCell = (GEInputTableViewCell*)[self.tableView getCellViewInRow:0 inSection:0];
    UITextField *textField01 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:0];
    GEPickerTextField *textField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:0];
    
    UITextField *textField03 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:3 inSection:0];
    UITextField *textField05 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:5 inSection:0];
    UITextField *textField06 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:6 inSection:0];
    UITextField *textField14 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:7 inSection:0];
    UITextField *textField15 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:8 inSection:0];
    
    UITextField *textField07 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:1];
    UITextField *textField08 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:1];
    UITextField *textField09 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:1];
    UITextField *textField10 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:3 inSection:1];
    
    UITextField *textField11 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:2];
    UITextField *textField12 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:2];
    UITextField *textField13 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:2];
    
    UISwitch *mSwitch = [self.tableView getCellSaveClass:[UISwitch class] pathForRow:4 inSection:0];
    NSString *HTBH = [self.orderInfo objectFormatStringForKey:@"合同编号"];
    
    NSMutableDictionary *dataDictionary = [NSMutableDictionary dictionary];
    [dataDictionary setFormatObject:HTBH forKey:@"合同编号"];
    [dataDictionary setFormatObject:geViewCell.objectData forKey:@"客户名称"];
    [dataDictionary setFormatObject:textField01.text forKey:@"合同名称"];
    [dataDictionary setFormatObject:textField.selectData forKey:@"合同性质"];
    [dataDictionary setFormatObject:textField03.text forKey:@"签订方量"];
    [dataDictionary setFormatObject:[NSNumber numberWithBool:mSwitch.on] forKey:@"方量控制"];
    [dataDictionary setFormatObject:textField05.text forKey:@"签订日期"];
    [dataDictionary setFormatObject:textField06.text forKey:@"建设单位"];
    [dataDictionary setFormatObject:textField14.text forKey:@"施工单位"];
    [dataDictionary setFormatObject:textField15.text forKey:@"第三方单位"];
    
    [dataDictionary setFormatObject:textField07.text forKey:@"工地地址" defaults:[self.orderInfo objectFormatStringForKey:@"工地地址"]];
    [dataDictionary setFormatObject:textField08.text forKey:@"运输距离" defaults:[self.orderInfo objectFormatStringForKey:@"运输距离"]];
    [dataDictionary setFormatObject:textField09.text forKey:@"工地联系人" defaults:[self.orderInfo objectFormatStringForKey:@"工地联系人"]];
    [dataDictionary setFormatObject:textField10.text forKey:@"工地联系电话" defaults:[self.orderInfo objectFormatStringForKey:@"工地联系电话"]];
    
    [dataDictionary setFormatObject:textField11.text forKey:@"业务人员" defaults:[self.orderInfo objectFormatStringForKey:@"业务人员"]];
    [dataDictionary setFormatObject:textField12.text forKey:@"业务员电话" defaults:[self.orderInfo objectFormatStringForKey:@"业务员电话"]];
    [dataDictionary setFormatObject:textField13.text forKey:@"备注" defaults:[self.orderInfo objectFormatStringForKey:@"备注"]];
    
    //天元定制功能
    if(!self.orderInfo){
        GEPickerTextField *pickerText09 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:9 inSection:0];
        GEPickerTextField *pickerText10 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:10 inSection:0];
        [dataDictionary setFormatObject:pickerText09.selectData forKey:@"编号类型"];
        [dataDictionary setFormatObject:pickerText10.selectData forKey:@"归属站点"];
    }
    
    MTCConcreteConfirmationTYDZController *view = [[MTCConcreteConfirmationTYDZController alloc] init];
    [view setValue:dataDictionary];
    [self.navigationController pushViewController:view animated:YES];
}


#pragma mark - UIAlertView

- (void)showAlertView:(NSString*)message
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    alert.alertViewStyle=UIAlertViewStyleDefault;
    [alert show];
}


#pragma mark
#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(self.orderInfo && section == 0) return 9;//修改
    else if(!self.orderInfo && section == 0) return 11;//通用方法
    
    else if(section == 1) return 4;
    else if(section == 2) return 3;
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"CreateContractViewController%ld%ld",indexPath.section,indexPath.row];
    
    //合同基本信息
    if(indexPath.section == 0){
        GEInputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell != nil) return cell;
        
        if(indexPath.row == 0){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"客户名称";
            cell.placeholder = @"请选择客户名称";
            if (self.orderInfo)
                cell.isMark = YES;
            cell.isEdit = NO;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            if(self.orderInfo) {
                NSMutableDictionary *clienterDic = [NSMutableDictionary dictionary];
                [clienterDic setObject:[self.orderInfo objectFormatIntegerNumberForKey:@"客户ID"] forKey:@"客户ID"];
                [clienterDic setObject:[self.orderInfo objectFormatIntegerNumberForKey:@"客户编号"] forKey:@"客户编号"];
                [clienterDic setObject:[self.orderInfo objectFormatStringForKey:@"客户名称"] forKey:@"客户名称"];
                cell.objectData = clienterDic;
                cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"客户名称"];
            }
            
        } else if(indexPath.row == 1){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"合同名称";
            cell.placeholder = @"请输入合同名称";
            cell.isMark = YES;
            cell.isEdit = YES;
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"合同名称"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 2){
            WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
            [content setInterfaceType:InterfaceTypeGroup];
            [content setModuleType:@"HTGL_JCXX_LIST"];
            [content setResultBackType:ResultBackTypeCommonUnordered];
            [content addParameterKey:@"Type" value:@1];
            
            cell = [[GEInputTableViewCell alloc] initWithSelectReuseIdentifier:CellIdentifier request:content];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"合同性质";
            cell.placeholder = @"请选择合同性质";
            cell.isMark = YES;
            //[cell addTarget:self action:@selector(textFieldDidChange:)];
            [cell setEnablePrevious:NO next:NO];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
            if (self.orderInfo) {
                GEPickerTextField *textField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:0];
                NSMutableDictionary *clienterDic = [NSMutableDictionary dictionary];
                NSInteger HTXZ = [self.orderInfo objectFormatIntegerForKey:@"Status"];
                if(HTXZ >0){ //合同性质必须大于0，默认1开始
                    [clienterDic setObject:[self.orderInfo objectFormatIntegerNumberForKey:@"Status"] forKey:@"Status"];
                    [clienterDic setObject:[self.orderInfo objectFormatStringForKey:@"合同性质"] forKey:@"Definition"];
                    textField.selectData = clienterDic;
                    cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"合同性质"];
                }
            }
            
        } else if(indexPath.row == 3){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"签订方量";
            cell.placeholder = @"请输入签订方量(立方)";
            cell.isMark = YES;
            cell.isEdit = YES;
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"签订方量"];
            [cell setKeyboardType:UIKeyboardTypeDecimalPad filter:@"0123456789" block:
             ^BOOL(UITextField *textField, NSRange range, NSString *string) {
                 //第二个不能是零,且第一个是零后自动清除
                 if (textField.text.length == 1 && [textField.text hasPrefix:@"0"]){
                     textField.text = string;
                     return NO;
                 }
                 return YES;
             }];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 4){
            cell = [[GEInputTableViewCell alloc] initWithSwitchIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"方量控制";
            cell.isMark = NO;
            if(self.orderInfo) {
                NSInteger FLKZ = [self.orderInfo objectFormatIntegerForKey:@"FLKZ"];
                UISwitch *mSwitch = [self.tableView getCellSaveClass:[UISwitch class] pathForRow:indexPath.row inSection:indexPath.section];
                mSwitch.on = FLKZ == 2 ? YES : NO;
            }
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 5){
            cell = [[GEInputTableViewCell alloc] initWithDateReuseIdentifier:CellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"签订日期";
            cell.placeholder = @"请选择签订日期";
            cell.isMark = YES;
            [cell setEnablePrevious:NO next:NO];
            [cell setDatePickerMode:UIDatePickerModeDate];
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"签订日期"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 6){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"建设单位";
            cell.placeholder = @"请输入建设单位";
            cell.isMark = NO;
            cell.isEdit = YES;
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"建设单位"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 7){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"施工单位";
            cell.placeholder = @"请输入施工单位";
            cell.isMark = YES;
            cell.isEdit = YES;
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"施工单位"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 8){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"第三方单位";
            cell.placeholder = @"请输入第三方单位";
            cell.isMark = NO;
            cell.isEdit = YES;
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"第三方单位"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
        }
        
        //天元定制功能
        /******************************************************************************************/
        else if(indexPath.row == 9){
            WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
            [content setInterfaceType:InterfaceTypeGroup];
            [content setModuleType:@"HTGL_XZHT_TYDZ_ZDXZ"];
            [content setResultBackType:ResultBackTypeCommonUnordered];
            [content addParameterKey:@"UType" value:@2];
            [content addParameterKey:@"UCode" value:@""];
            
            cell = [[GEInputTableViewCell alloc] initWithSelectReuseIdentifier:CellIdentifier request:content];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"编号类型";
            cell.placeholder = @"请选择编号类型";
            cell.isMark = YES;
            [cell setEnablePrevious:NO next:NO];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 10){
            cell = [[GEInputTableViewCell alloc] initWithSelectReuseIdentifier:CellIdentifier request:nil];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"归属站点";
            cell.placeholder = @"请选择归属站点";
            cell.isMark = YES;
            [cell setEnablePrevious:NO next:NO];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
        }
        /******************************************************************************************/
        
        return cell;
    }
    
    //工地信息
    else if(indexPath.section == 1){
        GEInputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell != nil) return cell;
        
        if(indexPath.row == 0){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"工地地址";
            cell.placeholder = @"请输入施工单位";
            cell.isMark = NO;
            cell.isEdit = YES;
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"工地地址"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 1){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"运输距离";
            cell.placeholder = @"请输入运输距离(公里)";
            cell.isMark = NO;
            cell.isEdit = YES;
            [cell setKeyboardType:UIKeyboardTypeDecimalPad filter:@"0123456789."];
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"运输距离"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 2){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"工地联系人";
            cell.placeholder = @"请输入工地联系人";
            cell.isMark = NO;
            cell.isEdit = YES;
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"工地联系人"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 3){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"工地联系电话";
            cell.placeholder = @"请输入联系电话";
            cell.isMark = NO;
            cell.isEdit = YES;
            [cell setKeyboardType:UIKeyboardTypeDecimalPad filter:@"0123456789+"];
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"工地联系电话"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        }
        return cell;
    }
    
    //其他信息
    else if(indexPath.section == 2){
        GEInputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell != nil) return cell;
        else if(indexPath.row == 0){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"业务人员";
            cell.placeholder = @"请输入业务人员";
            cell.isMark = NO;
            cell.isEdit = YES;
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"业务人员"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 1){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"业务员电话";
            cell.placeholder = @"请输入业务人员电话";
            cell.isMark = NO;
            cell.isEdit = YES;
            [cell setKeyboardType:UIKeyboardTypeDecimalPad filter:@"0123456789+"];
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"业务员电话"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 2){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"备        注";
            cell.placeholder = @"可不填";
            cell.isMark = NO;
            cell.isEdit = YES;
            if(self.orderInfo) cell.inputFiled = [self.orderInfo objectFormatStringForKey:@"备注"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
        }
        
        return cell;
    }
    
    return nil;
}

#pragma mark
#pragma mark - UITableViewDelegate

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if(indexPath.section == 0 && indexPath.row == 0){ //客户名称选择
        MTCSelectClienterViewController *viewController = [[MTCSelectClienterViewController alloc] init];
        viewController.backResultDelagate = self;
        AbstractNavigationController *navigationController = [[AbstractNavigationController alloc] initWithRootViewController:viewController];
        [self presentViewController:navigationController animated:YES completion:nil];
        
//        MTCSelectClienterViewController *view = [[MTCSelectClienterViewController alloc] init];
//        view.backResultDelagate = self;
//        [self presentViewController:view animated:YES completion:nil];
        
    } else if(indexPath.section == 0 && indexPath.row == 2){ //合同性质
        GEInputTableViewCell *geViewCell = (GEInputTableViewCell*)[self.tableView getCellViewInRow:2 inSection:0];
        NSArray *resultArray = geViewCell.objectData;
        if (resultArray) {
            NSDictionary *QDDJ = [NSDictionary dictionaryWithObject:@"%@" forKey:@"Definition"];
            [geViewCell setPickerTextFieldComponents:resultArray keyValue:@[QDDJ]];
        }
        
    } else if(indexPath.section == 0 && indexPath.row == 4){ //方量控制
        UISwitch *mSwitch = [self.tableView getCellSaveClass:[UISwitch class] pathForRow:indexPath.row inSection:indexPath.section];
        [mSwitch setOn:!mSwitch.on animated:YES];
        
    }
    
    //天元定制功能
    /******************************************************************************************/
    else if(indexPath.section == 0 && indexPath.row == 9){ //编号类型
        //[self distributeStionNetWorkContractData];
        GEInputTableViewCell *geViewCell = (GEInputTableViewCell*)[self.tableView getCellViewInRow:9 inSection:0];
        NSArray *resultArray = geViewCell.objectData;
        if (resultArray) {
            NSDictionary *QDDJ = [NSDictionary dictionaryWithObject:@"%@" forKey:@"U_name"];
            [geViewCell setPickerTextFieldComponents:resultArray keyValue:@[QDDJ]];
        }
        [geViewCell addDoneTarget:self action:@selector(TYDZDoneSelector:)];
        
        //键盘事件处理
        UITextField *textField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:indexPath.row inSection:indexPath.section];
        if (textField) [textField becomeFirstResponder];
        return;
        
    } else if(indexPath.section == 0 && indexPath.row == 10){ //归属站点
        //存在数据就直接选择
        GEInputTableViewCell *geViewCell10 = (GEInputTableViewCell*)[self.tableView getCellViewInRow:10 inSection:0];
        if(geViewCell10.objectData){
            //键盘事件处理
            UITextField *textField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:indexPath.row inSection:indexPath.section];
            if (textField) [textField becomeFirstResponder];
            return;
        }
        
        //不存在数据就网络获取
        GEInputTableViewCell *geViewCell = (GEInputTableViewCell*)[self.tableView getCellViewInRow:9 inSection:0];
        GEPickerTextField *textField1 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:9 inSection:0];
        if(geViewCell.objectData && textField1.selectData){
            NSDictionary *mDictionary = textField1.selectData;
            NSString *UCode = [mDictionary objectFormatStringForKey:@"U_code"];
            [self TYDZStionNetWorkContractData:UCode];
        } else{
            [self showAlertView:@"请先选择编号类型"]; return;
        }
        return;
    }
    /******************************************************************************************/
    
    //键盘事件处理
    UITextField *textField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:indexPath.row inSection:indexPath.section];
    if (textField) [textField becomeFirstResponder];
    
}


#pragma mark
#pragma mark - 天元定制--站点选择

/*****************************天元定制****************************************/

- (void)TYDZDoneSelector:(id)selector
{
    GEInputTableViewCell *geViewCell = (GEInputTableViewCell*)[self.tableView getCellViewInRow:10 inSection:0];
    GEPickerTextField *textField1 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:10 inSection:0];
    UITextField *textField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:10 inSection:0];
    
    geViewCell.objectData = nil;
    textField1.selectData = nil;
    textField.text = @"";
}

//归属站点
- (void)TYDZStionNetWorkContractData:(NSString*)UCode
{
    JGProgressHUD *HUD = [self showSimpleHUDText:@"获取中"];
    
    //UType解释：1归属站点 2编号类型
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeGroup];
    [content setModuleType:@"HTGL_XZHT_TYDZ_ZDXZ"];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    [content addParameterKey:@"UType" value:@1];
    [content addParameterKey:@"UCode" value:UCode];
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        [HUD dismissAnimated:YES];//关闭进度框
        
        //添加数据，刷新页面
        NSArray *firstArray = nil;
        if(result.resultArray && result.resultArray.count>0)
            firstArray = [result.resultArray firstObject];
        
        //数据处理
        if(firstArray && firstArray.count>0){
            GEInputTableViewCell *geViewCell = (GEInputTableViewCell*)[self.tableView getCellViewInRow:10 inSection:0];
            geViewCell.objectData = firstArray;
            NSDictionary *QDDJ = [NSDictionary dictionaryWithObject:@"%@" forKey:@"U_name"];
            [geViewCell setPickerTextFieldComponents:firstArray keyValue:@[QDDJ]];
            
            //键盘事件处理
            UITextField *textField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:10 inSection:0];
            if (textField) [textField becomeFirstResponder];
            
        } else {
            [self showErrorHUDText:@"无归属站点"];
        }
        
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [HUD dismissAnimated:YES];//关闭进度框
        [self showErrorHUDText:@"获取失败"];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [HUD dismissAnimated:YES];//关闭进度框
        [self showErrorHUDText:@"网络异常"];
        
    }];
}

/************************************************************************/

#pragma mark
#pragma mark - BackResultDataDelagate

- (void) backResultDataKey:(id)key dataValue:(id)backValue
{
    if (backValue) {
        //客户名称选择
        NSDictionary *clienterDictionary = backValue;
        GEInputTableViewCell *clienterCell = (GEInputTableViewCell*)[self.tableView getCellViewInRow:0 inSection:0];
        clienterCell.objectData = clienterDictionary;
        clienterCell.inputFiled = [clienterDictionary objectFormatStringForKey:@"客户名称"];
    }
}

@end
